//
//  GoodDeedDetailViewController.m
//  homeland
//
//  Created by gzbugu on 14-8-8.
//  Copyright (c) 2014年 xzysun. All rights reserved.
//  好人好事详情类
//

#import "GoodDeedDetailViewController.h"
#import "UIImageView+WebCache.h"
#import "GoodDeedEntity.h"
#import "UserInfoService.h"
#import "XHImageViewer.h"
#import "VoteMasterDetailViewController.h"
#import "SurveyDetailViewController.h"
#import <QuartzCore/QuartzCore.h>

//扩展属性
@interface GoodDeedDetailViewController () <XHImageViewerDelegate> {
    //当前页面－图片集
    NSMutableArray *_imageViews;
    //手势识别事件
    UITapGestureRecognizer *_gesture;
}
//变量区
//=========================
//定义一个变量来记录下一个将要添加的控件的位置
@property (assign, nonatomic) int nextY;
//调查主体id
@property (strong, nonatomic) NSString *masterId;
//好人好事实体
@property (strong, nonatomic) GoodDeedEntity *itemEntity;
@end

@implementation GoodDeedDetailViewController
//方法区
//=========================
-( id )initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self =[ super initWithNibName :nibNameOrNil bundle :nibBundleOrNil];
    if (self){
        
    }
    return self ;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"详情";
    [self configView];
}

/**数据渲染展示*/
-(void)configView{
    _nextY = 0;
    _imageViews = [NSMutableArray array];
    //主体内容展示
    //--------------------------
    _itemEntity= [_config objectForKey:@"dataItem"];
    
    //展示主体内容
    //-------------------
    NSString *_Vtitle = [CommonUtil dealNullObject:_itemEntity.title];
    NSString *_Vcontent = [CommonUtil dealNullObject:_itemEntity.content];
    //根据内容调整高度
    CGFloat titleHeight = [self caculateHeightForString:_Vtitle AndFontSize:16.0f AndWitdh:300.0f];
    
    self.titleTextView.frame = CGRectMake(self.titleTextView.frame.origin.x,self.titleTextView.frame.origin.y, self.titleTextView.frame.size.width, titleHeight);
    self.titleTextView.text = _Vtitle;
    
    //展示图片
    //--------------------
    NSString *pic = [CommonUtil dealNullObject:_itemEntity.imageUrl];
    pic= [NSString stringWithFormat:@"http://%@%@", SERVER_URL_GOODDEED_MY_NEWSPAPERS_IMAGE, pic];
        self.imageView.frame = CGRectMake(self.imageView.frame.origin.x,CGRectGetMaxY(self.titleTextView.frame) + 5.0, self.imageView.frame.size.width, self.imageView.frame.size.height);
        __block UIActivityIndicatorView *activityIndicator;
        __weak UIImageView *weakImageView = self.imageView;
        [self.imageView setImageWithURL:[NSURL URLWithString:pic] placeholderImage:[UIImage imageNamed:@"placeholder.png"] options:SDWebImageProgressiveDownload progress:^(NSUInteger receivedSize, long long expectedSize){
            if (!activityIndicator){
                [weakImageView addSubview:activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]];
                activityIndicator.center = weakImageView.center;
                [activityIndicator startAnimating];
            }
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType){
            [activityIndicator removeFromSuperview];
            activityIndicator = nil;
        }];
        //用于给图片绑定事件
        _gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHandle:)];
        self.imageView.userInteractionEnabled = YES;
        self.imageView.contentMode = UIViewContentModeScaleAspectFill;
        self.imageView.clipsToBounds = YES;
        [self.imageView addGestureRecognizer:_gesture];
        
        [_imageViews addObject:self.imageView];
        
    //间隔线
    self.spiritLine.frame = CGRectMake(self.spiritLine.frame.origin.x, CGRectGetMaxY(self.imageView.frame)+5.0,self.spiritLine.frame.size.width, 1);
    self.spiritLine.backgroundColor = [UIColor blackColor];
    
    //内容
     CGFloat contentHeight = [self caculateHeightForString:_Vcontent AndFontSize:14.0f AndWitdh:300.0f];
    self.contentTextView.frame = CGRectMake(self.contentTextView.frame.origin.x, CGRectGetMaxY(self.spiritLine.frame)+5.0, self.contentTextView.frame.size.width, contentHeight);
    self.contentTextView.text = _Vcontent;
    
    CGFloat totalHeight = MAX(CGRectGetHeight(self.contentTextView.frame)+10.0, CGRectGetHeight(self.scrollView.frame));
    self.mainPanel.frame = CGRectMake(0.0, 0.0, 320.0, totalHeight);
    [self.scrollView addSubview:self.mainPanel];
    self.scrollView.contentSize = self.mainPanel.frame.size;
}

//图片浏览事件
- (void)tapHandle:(UITapGestureRecognizer *)tap {
    XHImageViewer *imageViewer = [[XHImageViewer alloc] init];
    imageViewer.delegate = self;
    imageViewer.disableTouchDismiss = NO;
    [imageViewer showWithImageViews:_imageViews selectedView:(UIImageView *)tap.view];
}
- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
